package com.maizhi.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maizhi.controller.menuController;
import com.maizhi.dao.BaseDaoI;
import com.maizhi.httpmodel.EasyuiTreeNode;
import com.maizhi.httpmodel.DeptModel;
import com.maizhi.httpmodel.MenuModel;
import com.maizhi.model.Dept;
import com.maizhi.service.DeptServiceI;
import com.maizhi.service.MenuServiceI;
import com.maizhi.util.DeptComparator;
import com.maizhi.util.MenuComparator;

@Service("deptService")
public class DeptServiceImpl extends BaseServiceImpl<Dept> implements
		DeptServiceI {
	
	private Logger log = Logger.getLogger(DeptServiceImpl.class);
	
	BaseDaoI<Dept> baseDao;

	public BaseDaoI<Dept> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(BaseDaoI<Dept> baseDao) {
		this.baseDao = baseDao;
	}

	@Transactional(readOnly = true)
	public List<DeptModel> treeGrid(String id) {
		List<DeptModel> treegrid = new ArrayList<DeptModel>();
		String hql = "from Dept t where t.dept.id is null order by t.seq";
		if (id != null && !id.trim().equals("")) {
			hql = "from Dept t where t.dept.id = '" + id + "' order by t.seq";
		}
		List<Dept> Depts = baseDao.find(hql);
		for (Dept d : Depts) {
			DeptModel m = new DeptModel();
			BeanUtils.copyProperties(d, m);
			m.setIconCls(d.getImg());
			m.setState(d.getStatus());
			if (d.getDept()!= null) {
				m.setParentId(d.getDept().getId());
				m.setParentText(d.getDept().getText());
			}
			if (d.getDepts() != null && d.getDepts().size() > 0) {
				m.setState("closed");
			}
			treegrid.add(m);
		}
		return treegrid;
	}
	
	@Cacheable(value = "DeptCache", key = "'tree'+#id")
	@Transactional(readOnly = true)
	public List<EasyuiTreeNode> tree(String id) {
		String hql = "from Dept t where t.dept.id is null order by t.seq";
		if (id != null && !id.trim().equals("")) {
			hql = "from Dept t where t.dept.id ='" + id + "' order by t.seq";
		}
		List<Dept> Depts = baseDao.find(hql);
		List<EasyuiTreeNode> tree = new ArrayList<EasyuiTreeNode>();
		for (Dept d : Depts) {
			tree.add(tree(d, false));
		}
		return tree;
	}

	private EasyuiTreeNode tree(Dept d, boolean recursive) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(d.getId());
		node.setText(d.getText());
		node.setIconCls(d.getImg());
		Map<String, Object> attributes = new HashMap<String, Object>();
		node.setAttributes(attributes);
		if (d.getDepts() != null && d.getDepts().size() > 0) {
			node.setState("closed");
			if (recursive) {// 递归查询子节点
				List<Dept> syDeptList = new ArrayList<Dept>(d.getDepts());
				Collections.sort(syDeptList, new DeptComparator());// 排序
				List<EasyuiTreeNode> children = new ArrayList<EasyuiTreeNode>();
				for (Dept m : syDeptList) {
					EasyuiTreeNode t = tree(m, true);
					children.add(t);
				}
				node.setChildren(children);
			}
		}
		return node;
	}
	
	@CacheEvict(value = "DeptCache", allEntries = true)
	@Transactional 
	public DeptModel add(DeptModel m) {
		Dept d = new Dept();
		BeanUtils.copyProperties(m, d);
		d.setImg(m.getIconCls());
		d.setStatus("1");
		if (m.getParentId() != null && !m.getParentId().trim().equals("")) {
			Dept pm = baseDao.load(Dept.class, m.getParentId());
			d.setDept(pm);
		}
		baseDao.save(d);
		return m;
	}

	@CacheEvict(value = "DeptCache", allEntries = true)
	@Transactional 
	public void del(DeptModel m) {
		Dept d = baseDao.get(Dept.class, m.getId());
		if (d != null) {
			recursiveDelete(d);
		}
	}

	@Transactional 
	private void recursiveDelete(Dept d) {
		if (d.getDepts() != null && d.getDepts().size() > 0) {
			Set<Dept> syDepts = d.getDepts();
			for (Dept t : syDepts) {
				recursiveDelete(t);
			}
		}
		baseDao.delete(d);
	}

	@CacheEvict(value = "DeptCache", allEntries = true)
	@Transactional 
	public DeptModel edit(DeptModel m) {
		Dept d = baseDao.get(Dept.class, m.getId());
		if (m != null) {
			BeanUtils.copyProperties(m, d);
			d.setImg(m.getIconCls());
			if (!d.getId().equals("0")) {// 根节点不可以修改上级节点
				d.setDept(baseDao.get(Dept.class, m.getParentId()));
			}
		}
		return m;
	}
}
